﻿using TjZheng.Editor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TjZheng.Framework;
using TjZheng.Domain.Business;
using TjZheng.Domain.DataEntity;

namespace TjZheng.Example.MVC
{
     /// <summary>
    /// UEHandle 的摘要说明
    /// </summary>
    public class UEHandle : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            Handler action = null;
            if (context.Request["action"] == "config")
            {
                action = new ConfigHandler(context);
                action.Process();
                return;
            }

            BizUploadLimit objBizUploadLimit = new BizUploadLimit();
            UploadLimit objUploadLimit = null;
            long ulid = 0;
            if (context.Request["ULId"].IsNoNull() && long.TryParse(context.Request["ULId"], out ulid))
            {
                objUploadLimit = objBizUploadLimit.GetRecord(ulid);
            }
            else
            {
                if (context.Request["CategoryCode"].IsNoNull() && context.Request["LimitCode"].IsNoNull())
                {
                    objUploadLimit = objBizUploadLimit.GetRecord(string.Format("UcCode='{0}' AND LimitCode='{1}'", context.Request["CategoryCode"], context.Request["LimitCode"]));
                }
            }
            if (objUploadLimit == null)
            {
                action = new NotSupportedHandler(context) { ErrorMessage = "未设置上传文件配置" };
                action.Process();
                return;
            }
            if (objUploadLimit.UcCode.IsNull() || objUploadLimit.LimitCode.IsNull())
            {
                action = new NotSupportedHandler(context) { ErrorMessage = "上传文件的资源码和限制码不能为空" };
                action.Process();
                return;
            }
            switch (context.Request["action"])
            {
                case "uploadimage":
                    action = new UploadHandler(context, new UploadConfig(objUploadLimit)
                    {
                        UploadFieldName = Config.GetString("imageFieldName")
                    });
                    break;
                case "uploadscrawl":
                    action = new ScrawlHandle(context, new UploadConfig(objUploadLimit)
                    {
                        UploadFieldName = Config.GetString("scrawlFieldName"),
                        Base64Filename = "scrawl.png"
                    });
                    break;
                case "uploadvideo":
                    action = new UploadHandler(context, new UploadConfig()
                    {
                        AllowExtensions = Config.GetStringList("videoAllowFiles"),
                        PathFormat = Config.GetString("videoPathFormat"),
                        SizeLimit = Config.GetInt("videoMaxSize"),
                        UploadFieldName = Config.GetString("videoFieldName")
                    });
                    break;
                case "uploadfile":
                    action = new UploadHandler(context, new UploadConfig()
                    {
                        AllowExtensions = Config.GetStringList("fileAllowFiles"),
                        PathFormat = Config.GetString("filePathFormat"),
                        SizeLimit = Config.GetInt("fileMaxSize"),
                        UploadFieldName = Config.GetString("fileFieldName")
                    });
                    break;
                case "listimage":
                    action = new ListFileManager(context, Config.GetString("imageManagerListPath"), Config.GetStringList("imageManagerAllowFiles"));
                    break;
                case "listfile":
                    action = new ListFileManager(context, Config.GetString("fileManagerListPath"), Config.GetStringList("fileManagerAllowFiles"));
                    break;
                case "catchimage":
                    action = new CrawlerHandler(context, objUploadLimit);
                    break;
                default:
                    action = new NotSupportedHandler(context) { ErrorMessage = "action 参数为空或者 action 不被支持。" };
                    break;
            }
            action.Process();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}